package com.sap.sap_mangers.service.Impl;

import com.sap.sap_mangers.mapper.ActivityMapper;
import com.sap.sap_mangers.pojo.SapActivity;
import com.sap.sap_mangers.service.ActivityService;
import com.sap.sap_mangers.service.CacheService;
import com.sap.sap_mangers.until.CosUntil;
import com.sap.sap_mangers.until.SimpleJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author 樊东升
 * @date 2022/8/13 23:17
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private CacheService cacheService;

    @Override
    public Integer getActivity(Integer year) {
        return activityMapper.getActivity(year);
    }

    @Override
    public SimpleJson addActivity(SapActivity sapActivity, List<MultipartFile> files) {
        activityMapper.addActivity(sapActivity);
        return cosImage(sapActivity, files, false, null);

    }

    @Override
    public List<SapActivity> getActivityForYear(Integer year) {
        List<SapActivity> activityForYear = activityMapper.getActivityForYear(year);
        for (SapActivity sapActivity : activityForYear) {
            sapActivity.setImageUrl(cacheService.getActivityImage(String.valueOf(sapActivity.getActivityImageId())));
        }
        return activityForYear;
    }

    @Override
    public SimpleJson updateActivity(SapActivity sapActivity, List<MultipartFile> files) {
        SapActivity activityForOrder = activityMapper.getActivityForOrder((int) sapActivity.getActivityYear(), (int) sapActivity.getActivityOrder());
        long activityImageId = activityForOrder.getActivityImageId();
        activityMapper.deleteImageByImageId(activityImageId);
        activityMapper.updateActivity(sapActivity);
        return cosImage(sapActivity, files, true, activityImageId);
    }

    @Override
    public List<SapActivity> getActivityAll() {
        List<SapActivity> activityAll = activityMapper.getActivityAll();
        for (SapActivity sapActivity : activityAll) {
            List<String> activityImage = cacheService.getActivityImage(String.valueOf(sapActivity.getActivityImageId()));
            sapActivity.setImageUrl(activityImage);
        }
        return activityAll;
    }

    @Override
    public SapActivity getActivityForOrder(Integer year, Integer order) {
        SapActivity activityForOrder = activityMapper.getActivityForOrder(year, order);
        try {
            activityForOrder.setImageUrl(cacheService.getActivityImage(String.valueOf(activityForOrder.getActivityImageId())));
        } catch (NullPointerException e) {
            return activityForOrder;
        }
        return activityForOrder;
    }

    @Override
    public SapActivity getCurrentActivity() {
        SapActivity currentActivity = activityMapper.getCurrentActivity();
        try {
            currentActivity.setImageUrl(cacheService.getActivityImage(String.valueOf(currentActivity.getActivityImageId())));
        } catch (NullPointerException e) {
            return currentActivity;
        }
        return currentActivity;
    }

    /**
     * 把图片加入到活动图片表并上传到COS
     *
     * @param sapActivity 活动内容
     * @param files       图片组
     * @return 视图
     */
    private SimpleJson cosImage(SapActivity sapActivity, List<MultipartFile> files, Boolean isUpdate, Long activityImageId) {
        CosUntil cosUntil = new CosUntil();
        for (MultipartFile file : files) {
            try {
                String cosUrl = cosUntil.getCosUrl(file, "activityImage/" + file.getOriginalFilename());
                activityMapper.addImage(sapActivity.getActivityImageId(), cosUrl);
            } catch (IOException e) {
                e.printStackTrace();
                return new SimpleJson(1, "图片上传对象储存出错，请稍后再试！");
            }
        }
        if (isUpdate) {
            cacheService.updateActivityImage(activityMapper.getImageById(sapActivity.getActivityImageId()), String.valueOf(activityImageId), String.valueOf(sapActivity.getActivityImageId()));
        }
        SapActivity activityForOrder = activityMapper.getActivityForOrder((int) sapActivity.getActivityYear(), (int) sapActivity.getActivityOrder());
        activityForOrder.setImageUrl(cacheService.getActivityImage(String.valueOf(activityForOrder.getActivityImageId())));
        return new SimpleJson(0, "获取成功", activityForOrder);
    }
}
